/*
 * $Id: async_server.c,v 1.4 Broadcom SDK $
 * $Copyright: Copyright 2011 Broadcom Corporation.
 * This program is the proprietary software of Broadcom Corporation
 * and/or its licensors, and may only be used, duplicated, modified
 * or distributed pursuant to the terms and conditions of a separate,
 * written license agreement executed between you and Broadcom
 * (an "Authorized License").  Except as set forth in an Authorized
 * License, Broadcom grants no license (express or implied), right
 * to use, or waiver of any kind with respect to the Software, and
 * Broadcom expressly reserves all rights in and to the Software
 * and all intellectual property rights therein.  IF YOU HAVE
 * NO AUTHORIZED LICENSE, THEN YOU HAVE NO RIGHT TO USE THIS SOFTWARE
 * IN ANY WAY, AND SHOULD IMMEDIATELY NOTIFY BROADCOM AND DISCONTINUE
 * ALL USE OF THE SOFTWARE.  
 *  
 * Except as expressly set forth in the Authorized License,
 *  
 * 1.     This program, including its structure, sequence and organization,
 * constitutes the valuable trade secrets of Broadcom, and you shall use
 * all reasonable efforts to protect the confidentiality thereof,
 * and to use this information only in connection with your use of
 * Broadcom integrated circuit products.
 *  
 * 2.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS
 * PROVIDED "AS IS" AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES,
 * REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY,
 * OR OTHERWISE, WITH RESPECT TO THE SOFTWARE.  BROADCOM SPECIFICALLY
 * DISCLAIMS ANY AND ALL IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY,
 * NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES,
 * ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
 * CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING
 * OUT OF USE OR PERFORMANCE OF THE SOFTWARE.
 * 
 * 3.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL
 * BROADCOM OR ITS LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL,
 * INCIDENTAL, SPECIAL, INDIRECT, OR EXEMPLARY DAMAGES WHATSOEVER
 * ARISING OUT OF OR IN ANY WAY RELATING TO YOUR USE OF OR INABILITY
 * TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF
 * THE AMOUNT ACTUALLY PAID FOR THE SOFTWARE ITSELF OR USD 1.00,
 * WHICHEVER IS GREATER. THESE LIMITATIONS SHALL APPLY NOTWITHSTANDING
 * ANY FAILURE OF ESSENTIAL PURPOSE OF ANY LIMITED REMEDY.$
 *
 * Asynchronous BCM API Server
 *
 * Most of the support routines and header definitions for async api
 * routines are generated by mkdispatch:
 *
 * User access:
 * include/bcm/async.h		user declarations of bcm_*_async routines
 * src/bcm/async.c		bcm_*_async routines
 * include/bcmx/async.h		user declarations of bcmx_*_async routines
 * src/bcmx/async.c		bcmx_*_async routines
 *
 * Internal async server support:
 * include/bcm_int/async_req.h	bcm async request definition
 * include/bcm_int/xasync_req.h	bcmx async request definition
 * src/bcm/async_run.c		execution of bcm async request
 * src/bcmx/async_run.c		execution of bcmx async request
 *
 * bcmx and bcm async requests share a common header format that is used
 * by the routines in this file.
 *
 * This file provides the actual async execution server thread and
 * the queueing routines.
 */

#ifdef	BCM_ASYNC_SUPPORT

#include <sal/core/sync.h>
#include <sal/core/thread.h>
#include <shared/alloc.h>
#include <sal/appl/sal.h>	/* sal_dma_alloc, sal_dma_free */

#include <bcm/types.h>
#include <bcm_int/async_server.h>

#define	ASYNC_THREAD_STACK	SAL_THREAD_STKSZ
#define	ASYNC_THREAD_PRIO	255

#define	ASYNC_LOCK		sal_mutex_take(_async_lock, sal_mutex_FOREVER)
#define	ASYNC_UNLOCK		sal_mutex_give(_async_lock)
#define	ASYNC_SLEEP		sal_sem_take(_async_sem, sal_sem_FOREVER)
#define	ASYNC_WAKE		sal_sem_give(_async_sem)

static	sal_mutex_t	_async_lock;		/* lock access to _async_req */
static	sal_sem_t	_async_sem;		/* wait for async_adds */
static	sal_thread_t	_async_thread = SAL_THREAD_ERROR;
static	int		_async_thread_exit;

static	bcm_async_req_t	*_async_req_head;
static	bcm_async_req_t	*_async_req_tail;

/*
 * Add an async request to the end of the queue
 */
void
bcm_async_add(bcm_async_req_t *req)
{
    if (req == NULL) {
	return;
    }
    if (_async_thread == SAL_THREAD_ERROR) {
	if (req->callback != NULL) {
	    (*req->callback)(req->cookie, BCM_E_UNAVAIL);
	}
	sal_free(req);
	return;
    }
    ASYNC_LOCK;
    if (_async_req_head == NULL) {		/* empty */
	_async_req_head = _async_req_tail = req;
    } else {
	_async_req_tail->next = req;
	_async_req_tail = req;
    }
    ASYNC_UNLOCK;
    ASYNC_WAKE;
}

/*
 * Async Server thread.
 * Pull an entry off the front of the queue and run it.
 */
STATIC void
_bcm_async_thread(void *cookie)
{
    bcm_async_req_t	*req, *nreq;

    COMPILER_REFERENCE(cookie);

    _async_thread_exit = 0;
    for (;;) {
	ASYNC_SLEEP;

	/* grab a set of requests off the queue */
	ASYNC_LOCK;
	req = _async_req_head;
	_async_req_head = _async_req_tail = NULL;
	ASYNC_UNLOCK;

	/* run the requests */
	while (req != NULL) {
	    nreq = req->next;
	    (*req->run)(req);		/* bcm_async_run or bcmx_async_run */
	    req = nreq;
	}
	if (_async_thread_exit) {
	    _async_thread = SAL_THREAD_ERROR;
	    sal_thread_exit(0);
	    return;
	}
    }
}

/*
 * Start the Async thread
 */
int
bcm_async_start(void)
{
    if (_async_thread != SAL_THREAD_ERROR) {
	return BCM_E_BUSY;
    }
    _async_lock = sal_mutex_create("bcm_async");
    _async_sem = sal_sem_create("bcm_async", sal_sem_BINARY, 0);
    _async_thread = sal_thread_create("bcmASYNC",
				      ASYNC_THREAD_STACK,
				      ASYNC_THREAD_PRIO,
				      _bcm_async_thread,
				      NULL);
    if (_async_thread == SAL_THREAD_ERROR) {
	sal_sem_destroy(_async_sem);
	sal_mutex_destroy(_async_lock);
	_async_lock = NULL;
	return BCM_E_RESOURCE;
    }
    return BCM_E_NONE;
}

/*
 * Stop the Async thread
 */
int
bcm_async_stop(void)
{
    if (_async_thread == SAL_THREAD_ERROR) {
	return BCM_E_NONE;
    }
    _async_thread_exit = 1;
    ASYNC_WAKE;
    sal_thread_yield();
    while (_async_thread != SAL_THREAD_ERROR) {
	ASYNC_WAKE;
	sal_usleep(10000);
    }
    sal_sem_destroy(_async_sem);
    sal_mutex_destroy(_async_lock);
    _async_lock = NULL;
    return BCM_E_NONE;
}

#endif	/* BCM_ASYNC_SUPPORT */

int _bcm_async_server_not_empty;
